package com.rock.code.bigdata.targetstop.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.bigdata.forecast.service.ForeCastService;
import com.rock.code.bigdata.targetstop.service.TargetsTopService;
import com.rock.code.common.comm.DES3;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 指标主机top排名
 *
 * @author piandu
 * @date 2018/11/27
 */
@Controller
@RequestMapping("/Top")
public class TargetsTopController extends BaseController {

    @RequestMapping("")
    public String queryTargetsTopView() {
        return "bigdata/targetstop/targetsTopView";
    }

    @Resource
    private TargetsTopService TargetsTopImpl;

    //指标top查询
    @ResponseBody
    @RequestMapping(value = "/targetsTopQuery", method = RequestMethod.POST)
    public List targetsTopQuery() throws Exception {
        List list = new ArrayList();
        try {
            list = TargetsTopImpl.targetsTop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    //主机top由高到低展示
    @ResponseBody
    @RequestMapping(value = "/queryGraph", method = RequestMethod.POST)
    public HashMap queryGraph(String DATE_TYPE, String kpiId) {
        HashMap map = new HashMap();
        HashMap resultMap = new HashMap();
        List list = new ArrayList();
//        List listtwo = new ArrayList();
//        List listthree = new ArrayList();
        if ("showWeek".equals(DATE_TYPE)) {
            //先确定上周是这年的第几周，再去周表查出这周的天数，再去月表根据日期查出使用率
            Map map1 = new HashMap();
//            map1.put("lastweek", TargetsTopImpl.lastWeek());
            map1.put("lastweek", "1");
            map1.put("kpiId", kpiId);
//            map1.put("tableName", TargetsTopImpl.weekTableName());
            map1.put("tableName", "tb_kpi_value_week_2018");
            List list1 = TargetsTopImpl.queryDateByWeek(map1);//根据上周第几周查询该周从几号到几号
            String str = "";
            for (Object o : list1) {
//                System.out.println(o.toString());
                str = o.toString();
            }
            String a = str.split(",")[0];
            String b = str.split(",")[1];
            String list_start = a.split("=")[1];//开始时间
            System.out.println(list_start);
            String d = b.split("=")[1];
            String list_end = d.replace("}", "");//结束时间
            System.out.println(list_end);
            /****表名***/
            String startTime = list_start.split("-")[0] + list_start.split("-")[1];
            String tableName_one = "tb_kpi_value_month_" + startTime;
            String endTime = list_end.split("-")[0] + list_end.split("-")[1];
            String tableName_two = "tb_kpi_value_month_" + endTime;

//            map.put("table_one", tableName_one);
            map.put("table_one", "tb_kpi_value_month_201801");
//            map.put("table_two", tableName_two);
            map.put("table_two", "tb_kpi_value_month_201801");
//            map.put("start_time", list_end);
            map.put("start_time", "2018-01-01");
//            map.put("end_time", list_start);
            map.put("end_time", "2018-01-31");
            map.put("kpiId", kpiId);
            List lastweek = TargetsTopImpl.queryLineByDate(map);
//            List lastweek1 = TargetsTopImpl.queryLineByDate(map);
//            List lastweek2 = TargetsTopImpl.queryLineByDate(map);
            resultMap.put("list", lastweek);
//            resultMap.put("listtwo", lastweek1);
//            resultMap.put("listthree", lastweek2);
            return resultMap;
        } else if ("showMonth".equals(DATE_TYPE)) {
            String tableName = TargetsTopImpl.monthYearTableName();//201807
//            map.put("tableName", tableName);
            String lastMonth = TargetsTopImpl.lastMonth();
            map.put("tableName", "tb_kpi_value_year_2018");
            map.put("kpiId", kpiId);
//            map.put("DATA_TIME", lastMonth);
            map.put("DATA_TIME", "2018-01");
            list = TargetsTopImpl.queryDataMonth(map);
//            listtwo = TargetsTopImpl.queryDataMonth(map);
//            listthree = TargetsTopImpl.queryDataMonth(map);
            resultMap.put("list", list);
//            resultMap.put("listtwo", listtwo);
//            resultMap.put("listthree", listthree);
        } else if ("showYear".equals(DATE_TYPE)) {
            String tableName = TargetsTopImpl.yearTableName();//2018
//            map.put("tableName", tableName);
            map.put("tableName", "tb_kpi_value_year_2018");
            map.put("kpiId", kpiId);
            list = TargetsTopImpl.queryDataYear(map);
//            listtwo = TargetsTopImpl.queryDataYear(map);
//            listthree = TargetsTopImpl.queryDataYear(map);
            resultMap.put("list", list);
//            resultMap.put("listtwo", listtwo);
//            resultMap.put("listthree", listthree);
        } else {
            String tableName = TargetsTopImpl.monthTableName();
            String dayName = TargetsTopImpl.day();
//            map.put("tableName", tableName);
//            map.put("dayName", dayName);
            map.put("tableName", "tb_kpi_value_month_201801");
            map.put("DATA_TIME", "2018-01-01");
            map.put("kpiId", kpiId);
            list = TargetsTopImpl.queryData(map);
//            listtwo = TargetsTopImpl.queryData(map);
//            listthree = TargetsTopImpl.queryData(map);
            resultMap.put("list", list);
//            resultMap.put("listtwo", listtwo);
//            resultMap.put("listthree", listthree);
        }
        return resultMap;
    }
}